package com.csoa.community.activity;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import android.content.Intent;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.collect.community.R;
import com.csoa.community.adapter.YFTableAdapter;
import com.csoa.community.bean.YFInfoBean;
import com.csoa.community.datepicker.OnWheelChangedListener;
import com.csoa.community.datepicker.WheelView;
import com.csoa.community.datepicker.adapter.NumericWheelAdapter;
import com.csoa.community.db.YFInfoDB;
import com.csoa.community.utils.StringUtils;
import com.csoa.community.widget.PullToRefreshListView;

public class YFInfoTableActivity extends BaseActivity {

	private ListView mList;
	public static List<YFInfoBean> mYFInfos = null;
	private YFTableAdapter adapter;
	private EditText etYFNumberTable;
	private EditText etYFAreaTable;
	private EditText tvYFDateTable;
	private View mViewDatePicker;
	private Animation mAnimationPushUpIn;
	private Animation mAnimationPushUpOut;
	private int START_YEAR;
	private int END_YEAR;
	private PullToRefreshListView mPullToRefreshListView;

	@Override
	public void initUI() {

		setContentView(R.layout.yf_info_table);
		etYFNumberTable = (EditText)this.findViewById(R.id.etYFNumberTable);
		etYFAreaTable = (EditText)this.findViewById(R.id.etYFAreaTable);
		tvYFDateTable = (EditText)this.findViewById(R.id.etYFDateTable);
		tvYFDateTable.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				showDateTimePicker(tvYFDateTable);
			}
		});
		tvYFDateTable.setOnFocusChangeListener(new OnFocusChangeListener() {
			
			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				showDateTimePicker(tvYFDateTable);
			}
		});
		
		mViewDatePicker = this.findViewById(R.id.layout_datepicker);

		mAnimationPushUpIn = AnimationUtils.loadAnimation(this,
				R.anim.slide_up_in);
		mAnimationPushUpOut = AnimationUtils.loadAnimation(this,
				R.anim.slide_down_out);
		
		this.findViewById(R.id.btnAddYF).setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						Intent intent = new Intent(YFInfoTableActivity.this,
								YFInfoActivity.class);
						intent.putExtra("which", 1);
						startActivityForResult(intent, 0);
					}
				});
		
		this.findViewById(R.id.btnQueryYF).setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						String yfNumber = etYFNumberTable.getText().toString().trim();
						String yfArea = etYFAreaTable.getText().toString().trim();
						String yfDate = tvYFDateTable.getText().toString().trim();
						if(StringUtils.isEmpty(yfNumber) && StringUtils.isEmpty(yfArea) && StringUtils.isEmpty(yfDate)) {
							YFInfoTableActivity.this.showToast("请输入任一搜索条件");
							return;
						}
						mYFInfos.clear();
						List<YFInfoBean> yfInfos = YFInfoDB.query(yfNumber,yfArea,yfDate);
						if (yfInfos == null || yfInfos.size() == 0) {
							YFInfoTableActivity.this.showToast("您搜索的样方信息不存在");
						}else {
							mYFInfos.addAll(yfInfos);
						}
						adapter.notifyDataSetChanged();
					}
				});
		this.findViewById(R.id.btnRefreshYF).setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						refresh();
					}
				});
		this.findViewById(R.id.btnBackYFTable).setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						finish();
					}
				});
		
		mPullToRefreshListView = (PullToRefreshListView) this.findViewById(R.id.listview);
		mPullToRefreshListView.setMode(PullToRefreshListView.MODE_NO);
		mList = mPullToRefreshListView.getRefreshableView();
		


	}

	@Override
	public void initData() {
		mYFInfos = new ArrayList<YFInfoBean>();
		List<YFInfoBean> yfInfos = YFInfoDB.query();
		if (yfInfos != null) {
			mYFInfos.addAll(yfInfos);
		}
		adapter = new YFTableAdapter(this, mYFInfos);
		mList.setAdapter(adapter);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
		if (resultCode == RESULT_OK) {
			if(intent != null) {
				YFInfoBean delYFInfo = (YFInfoBean)intent.getSerializableExtra("delete");
				if(delYFInfo != null) {//删除
					int position = intent.getIntExtra("position", 0);
					mYFInfos.remove(position);
				}else {
					YFInfoBean updateYFInfo = (YFInfoBean)intent.getSerializableExtra("update");
					if(updateYFInfo != null) {
						int position = intent.getIntExtra("position", 0);
						if(updateYFInfo != null) {//更新
							mYFInfos.remove(position);
							mYFInfos.add(position, updateYFInfo);
						}
					}else {//添加
						refresh();
						return;
					}
				}
			}else {
				refresh();
				return;
			}
			
			adapter.notifyDataSetChanged();
		}
	}
	
	public void refresh() {
		List<YFInfoBean> yfInfos = YFInfoDB.query();
		if(yfInfos != null) {
			mYFInfos.clear();
			mYFInfos.addAll(yfInfos);
			clearEdit();
			adapter.notifyDataSetChanged();
		}
	}
	
	public void clearEdit() {
		etYFNumberTable.setText("");
		etYFAreaTable.setText("");
		tvYFDateTable.setText("");
	}
	
	
	/**
	 * @Description: TODO 弹出日期时间选择器
	 */
	private void showDateTimePicker(final TextView tv) {
		Date dateInput = new Date(System.currentTimeMillis());
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(dateInput);
		int year = calendar.get(Calendar.YEAR);
		int month = calendar.get(Calendar.MONTH);
		int day = calendar.get(Calendar.DATE);
		View view = findViewById(R.id.layout_datepicker);

		// 年
		final WheelView wv_year = (WheelView) view.findViewById(R.id.year);
		START_YEAR = year - 3;
		END_YEAR = year + 8;
		wv_year.setViewAdapter(new NumericWheelAdapter(this, START_YEAR,
				END_YEAR, null, "年"));// 设置"年"的显示数据
		wv_year.setCurrentItem(year - START_YEAR);// 初始化时显示的数据

		// 月
		final WheelView wv_month = (WheelView) view.findViewById(R.id.month);
		wv_month.setViewAdapter(new NumericWheelAdapter(this, 1, 12, null, "月"));
		wv_month.setCyclic(true);
		wv_month.setCurrentItem(month);

		// 日
		final WheelView wv_day = (WheelView) view.findViewById(R.id.day);
		updateDays(START_YEAR, wv_year, wv_month, wv_day);
		wv_day.setCyclic(true);
		wv_day.setCurrentItem(day - 1);


		// 添加"年"监听
		OnWheelChangedListener wheelListener_year = new OnWheelChangedListener() {
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				updateDays(START_YEAR, wv_year, wv_month, wv_day);
			}
		};
		// 添加"月"监听
		OnWheelChangedListener wheelListener_month = new OnWheelChangedListener() {
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				updateDays(START_YEAR, wv_year, wv_month, wv_day);
			}
		};
		wv_year.addChangingListener(wheelListener_year);
		wv_month.addChangingListener(wheelListener_month);

		Button btn_sure = (Button) view.findViewById(R.id.btn_datetime_sure);
		Button btn_cancel = (Button) view
				.findViewById(R.id.btn_datetime_cancel);
		// 确定
		btn_sure.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				// 如果是个数,则显示为"02"的样式
				String parten = "00";
				DecimalFormat decimal = new DecimalFormat(parten);

				StringBuilder time = new StringBuilder();
				time.append(wv_year.getCurrentItem() + START_YEAR);
				time.append("-");
				time.append(decimal.format(wv_month.getCurrentItem() + 1));
				time.append("-");
				time.append(decimal.format(wv_day.getCurrentItem() + 1));
				time.append(" ");
				tv.setText(time.toString());
				showDatePicker();
			}
		});
		// 取消
		btn_cancel.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				showDatePicker();
			}
		});
		showDatePicker();

	}

	void updateDays(int startYear, WheelView year, WheelView month,
			WheelView day) {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.YEAR, startYear + year.getCurrentItem());
		calendar.set(Calendar.MONTH, month.getCurrentItem());

		int maxDays = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
		day.setViewAdapter(new NumericWheelAdapter(this, 1, maxDays, null, "日"));
		// int curDay = Math.min(maxDays, day.getCurrentItem() + 1);
		// day.setCurrentItem(curDay - 1, true);
	}

	private void showDatePicker() {
		if (mViewDatePicker.getVisibility() == View.GONE) {
			mViewDatePicker.setVisibility(View.VISIBLE);
			mViewDatePicker.startAnimation(mAnimationPushUpIn);
		} else {
			mViewDatePicker.setVisibility(View.GONE);
			mViewDatePicker.startAnimation(mAnimationPushUpOut);
		}
	}

	public String getCurrentTime() {
		Date dateInput = new Date(System.currentTimeMillis());
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(dateInput);
		int year = calendar.get(Calendar.YEAR);
		int month = calendar.get(Calendar.MONTH);
		int day = calendar.get(Calendar.DATE);
		return year + "-" + month + "-" + day;
	}
	
	

}
